﻿<%@ WebHandler Language="C#" Class="login" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间


public class login : IHttpHandler, IRequiresSessionState
{
    // 声明成员属性
    public void ProcessRequest(HttpContext context)
    {

        context.Response.Write(formDataUpload(context));
    }

    private static string formDataUpload(HttpContext context)
    {
        try
        {
            HttpFileCollection postedFile = context.Request.Files;
            bool fileUploaded = false;
            string name = context.Request.Form["name"];
            string author = context.Request.Form["author"];
            string uptime = context.Request.Form["uptime"];
            string type = context.Request.Form["type"];
            string intro = context.Request.Form["intro"];
            string mid = context.Request.Form["mid"];
            string isrecommend = context.Request.Form["isrecommend"]; 
            string imgPath = "";
            string videoPath = "";

            string msg = string.Empty;
            string error = string.Empty;

            // "++"

            if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(author) || String.IsNullOrEmpty(uptime) || String.IsNullOrEmpty(type) || String.IsNullOrEmpty(intro))
            {
                return "{\"error\":\"2\", \"msg\":\"表单项不能为空，请重新填写！\"}";
            }

            if (postedFile.Count > 0)
            {
                string savepath = "";
                string tempPath = "";
                tempPath = System.Web.HttpContext.Current.Server.MapPath("..\\upload\\");//System.Configuration.ConfigurationManager.AppSettings["FolderPath"];  
                savepath = tempPath; //context.Server.MapPath(tempPath);  

                string del_sql = "select img, url  from movielist where id =" + mid + "";
                DataSet delDs = DBHelper.sqlMethod(del_sql);

                
                for (int i = 0; i < postedFile.Count; i++)
                {
                    
                    string filename = postedFile[i].FileName;
                    if (String.IsNullOrEmpty(filename))
                    {
                        continue;
                    }

                    string sExtension = filename.Substring(filename.LastIndexOf('.'));

                    if (!Directory.Exists(savepath))
                        Directory.CreateDirectory(savepath);

                    // string sNewFileName = filename.Substring(0, filename.IndexOf("."));


                    string sNewFileName = Guid.NewGuid().ToString("N");
                    if (i == 0)
                    {
                        System.IO.File.Delete(context.Server.MapPath("~/" + delDs.Tables[0].Rows[0]["img"].ToString() + ""));
                        imgPath = @"upload/" + sNewFileName + sExtension;
                    }
                    else
                    {
                        System.IO.File.Delete(context.Server.MapPath("~/" + delDs.Tables[0].Rows[0]["url"].ToString() + ""));
                        videoPath = @"upload/" + sNewFileName + sExtension;
                    }
                    postedFile[i].SaveAs(savepath + @"\" + sNewFileName + sExtension);
                    msg = "修改成功! ";
                    error = "0";
                    fileUploaded = true;
                }

            }
            else
            {
                msg = "没有选择文件";
                error = "1";
                return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
            }
            
            string sql = " update movielist set mname ='" + name + "' ,maker ='" + author + "',onlinetime ='" + uptime + "' ,breif = '" + intro + "',tid = " + type + ", isrecommend = " + isrecommend + " ";
            
            if (imgPath != "")
            {
                sql += ",img = '" + imgPath + "' ";
            }
            
            if (videoPath != "")
            {
                sql += ",url = '" + videoPath + "' ";
            }
            
            sql += " where id = " + mid + " ";
            
            if (fileUploaded)
            {
                DBHelper.sqlMethodNoDataset(sql);
            }
            
            return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
            
        }
        catch (Exception e)
        {
            return "{\"error\":\"2\", \"msg\":\"发生错误，请重试\"}";
        }



    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}